const app = getApp();
const db = wx.cloud.database();
const _ = db.command;
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    CustomBar: app.globalData.CustomBar,
    period: null,
    listWeek: [],
    listMonth: [],
    // tab 切换
    TabCur: 0,
    // 偶像总数
    idolCount: 0,
    // 翻页加载
    page: 1,
    size: 20,

    page2: 1,
    size2: 20,

    swiperList: [{
      id: 0,
      type: 'image',
      url: 'cloud://dpsf-wsakw.6470-dpsf-wsakw-1300147729/idol/photo/1621064843762_0.png'
    }, {
      id: 1,
      type: 'image',
      url: 'cloud://dpsf-wsakw.6470-dpsf-wsakw-1300147729/idol/photo/1621065443769_1.png',
    }, {
      id: 2,
      type: 'image',
      url: 'cloud://dpsf-wsakw.6470-dpsf-wsakw-1300147729/idol/photo/1621269787190_2.png'
    }],
  },
  attached: function () {
    let that = this;
    wx.showLoading({
      title: '数据加载中',
      mask: true,
    })

    // 加载周期信息
    app.getPeriod(function (period) {
      console.log("加载周期")
      console.log(period)
      that.setData({
        period: period
      })
      console.log('rankUsers')
      that.loadIdol(true);
      wx.hideLoading();

    });
  },
  //组件的方法，包括事件响应函数和任意的自定义方法，关于事件响应函数的使用
  methods: {
    //分享
    onShareAppMessage: function () {
      wx.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline']
      })
    },
    //打开详情
    idolTap: function (e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/idol/detail?id=' + id
      })
    },
    loadIdol: function (isNew) {
      // 分发周榜-月榜
      let TabCur = this.data.TabCur;
      let that = this;
      db.collection('ad_idol').where({
        hasdel: 0
      }).count({
        success: res2 => {
          const total1 = res2.total
          that.setData({
            idolCount: total1
          })
          if (1 == TabCur) {
            that.loadMonth(isNew);
          } else {
            that.loadWeek(isNew);
          }
        }
      })

    },
    // 翻页加载 偶像信息
    loadWeek: function (isNew) {
      console.log("取偶像-周榜分页")
      let that = this, data = this.data;
      const total1 = data.idolCount;
      let size = data.size,
        page = data.page,
        week = data.period.week,
        total = Math.ceil(total1 / size);
      if (isNew == true) {
        that.setData({
          page: 1
        })
        page = 1
      }
      if (page <= total) {
        console.log("total=" + total)
        wx.cloud.callFunction({
          name: 'rankWeek',
          data: {
            week: week,
            skip: ((page - 1) * size),
            limit: size
          }
        }).then(kkk => {
          page++
          let list = kkk.result.list,
            length = list.length,
            idols = data.listWeek;
          // idolsID = that.data.idolsID;
          if (isNew == true) {
            idols = []
            // idolsID = {}
          }
          if (length > 0) {
            for (let i = 0; i < length; i++) {
              idols.push(list[i]);
              // if (!idolsID['idol_' + list[i]._id]) {
              //   var item = list[i].idol[0];
              //   item.hasvote = list[i].hasvote;
              //   idols.push(item)
              //   idolsID['idol_' + list[i]._id] = 1
              // }
            }
          }
          that.setData({
            listWeek: idols,
            // idolsID: idolsID,
            page: page
          })
        })
          .catch(err => console.error(err))
      }

    },
    // 翻页加载 偶像信息
    loadMonth: function (isNew) {
      console.log("取偶像-月榜榜分页")
      let that = this, data = this.data;
      const total1 = data.idolCount;
      let size = data.size2,
        page = data.page2,
        month = data.period.month,
        total = Math.ceil(total1 / size);
      if (isNew == true) {
        that.setData({
          page2: 1
        })
        page = 1
      }
      if (page <= total) {
        console.log("total=" + total)
        wx.cloud.callFunction({
          name: 'rankMonth',
          data: {
            month: month,
            skip: ((page - 1) * size),
            limit: size
          }
        }).then(kkk => {
          page++
          let list = kkk.result.list,
            length = list.length,
            idols = data.listMonth;
          // idolsID = that.data.idolsID;
          if (isNew == true) {
            idols = []
            // idolsID = {}
          }
          if (length > 0) {
            for (let i = 0; i < length; i++) {
              idols.push(list[i]);
            }
          }
          that.setData({
            listMonth: idols,
            // idolsID: idolsID,
            page2: page
          })
        })
          .catch(err => console.error(err))
      }
    },

    tabSelect(e) {
      console.log(e);
      let that = this;
      let TabCur = e.currentTarget.dataset.id;
      this.setData({
        TabCur: TabCur,
        scrollLeft: (e.currentTarget.dataset.id - 1) * 60
      })
      if (1 == TabCur) {
        if (!(that.data.listMonth.length > 0)) that.loadMonth(true);
      } else {
        if (!(that.data.listWeek.length > 0)) that.loadWeek(true);
      }
    },
    DotStyle(e) {
      this.setData({
        DotStyle: e.detail.value
      })
    },
    // cardSwiper
    cardSwiper(e) {
      this.setData({
        cardCur: e.detail.current
      })
    },

    // 内部方法建议以下划线开头
    _swichNav: function (e) {
      //自定义组件触发事件时，需要使用 triggerEvent 方法，指定事件名、detail对象和事件选项
      this.triggerEvent('changeCurrent', {
        currentNum: e.currentTarget.dataset.current
      })
    }
  }
})